package group.sicher.security.common.impl;

import group.sicher.security.common.AccessMatch;
import group.sicher.security.common.MarsAccessUriProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: aron
 * @date: 2019-08-06 16:29
 */
@Component
public class MarsAccessMatchImpl implements AccessMatch {

    private static final String ROOT_URI = "/";
    private static final String INDEX_URI = "/index.html";
    private static final String LOGIN_URI = "/login.html";
    private static final String SWAGGER_UI_HTML = "/swagger-ui.html";

    @Autowired
    private MarsAccessUriProperties urls;

    private List<String> accessList = new ArrayList<String>() {
        {
            add("/hx/login");
            add("/shop/login");
            add("/hx/loginDev");
            add("/hx/open/api/getSign");
            add(ROOT_URI);
            add(INDEX_URI);
            add(LOGIN_URI);
        }
    };

    private List<String> accessPattenList = new ArrayList<String>() {
        {
            add(SWAGGER_UI_HTML);
            add("/swagger-resources");
            add("/images/");
            add("/webjars/");
            add("/v2/api-docs");
            add("/configuration/ui");
            add("/configuration/security");
        }
    };

    private List<String> accessSignList = new ArrayList<String>() {
        {
            add("/hx/open/api/createOrder");
            add("/hx/monitor/updateOrder");
        }
    };

    @Override
    public boolean isAccessSignUri(HttpServletRequest request) {
        String requestUri = request.getRequestURI();
        String contextPath = request.getContextPath();
        List<String> accessSignPattenUriList = urls.getAccessSignPattenUriList();
        if (CollectionUtils.isEmpty(accessSignPattenUriList)) {
            return false;
        }
        for (String accessUri : accessSignPattenUriList) {
            if (null != requestUri && requestUri.startsWith(contextPath + accessUri)) {
                return false;
            }
        }
        return true;
    }

}
